// personal/collect/index.js
import {
  toast,
  distance
} from '../../utils/util'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabIndex: 0,
    iDisplayStart: 0,
    iDisplayLength: 10,
    rent: [],
    sell: [],
    maintain: [],
    isEmpty: false,
    enterprise: [],
    maintainTotal: 0,
    sellTotal:0,
    rentTotal:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getRentCollect()
    this.getSellCollect()
    this.getMaintainCollect()
  },
  getList() {
    const index = this.data.tabIndex
    Number(index) === 0 ? this.getRentCollect() : Number(index) === 1 ? this.getSellCollect() : this.getMaintainCollect()
  },
  getRentCollect() {
    const {
      iDisplayStart,
      iDisplayLength,
      tabIndex,
      rent
    } = this.data
    app.$api.collect.collectFind({
      iDisplayStart,
      iDisplayLength,
      infortype: 1
    }).then((res) => {
      const data = res.data.data
      const info = data.map((item) => {
        return this.calcDistance(item)
      })
      const newRent = rent.concat(info)
      this.setData({
        rent: newRent,
        isEmpty: Boolean(newRent.length),
        rentTotal: res.data.total
      })
    }).catch((e) => {
      toast('请稍后再试', 1000)
    })
  },
  getSellCollect() {
    const result = []
    const {
      iDisplayStart,
      iDisplayLength,
      tabIndex,
      sell
    } = this.data
    app.$api.collect.collectFind({
      iDisplayStart,
      iDisplayLength,
      infortype: 2
    }).then((res) => {
      const data = res.data.data
      const info = data.map((item) => {
        item.imgurl = item.equimgurl
        app.$api.common.commonFindName({
            code: item.enttype
          })
          .then((s) => {
            const data = s.data
            result.push(data.name)
            this.setData({
              enterprise: result
            })
          }).catch((e) => {
            toast('请稍后再试', 1000)
          })
        return this.calcDistance(item)
      })
      const newRent = sell.concat(info)
      this.setData({
        sell: newRent,
        isEmpty: Boolean(newRent.length),
        sellTotal: res.data.total
      })
    }).catch((e) => {
      toast('请稍后再试', 1000)
    })
  },
  getMaintainCollect() {
    const {
      iDisplayStart,
      iDisplayLength,
      tabIndex,
      maintain
    } = this.data
    app.$api.collect.collectFind({
      iDisplayStart,
      iDisplayLength,
      infortype: 3
    }).then((res) => {
      const data = res.data.data
      const info = data.map((item) => {
        item.imgurl = item.equimgurl
        item.maintainscope = item.equtype
        return this.calcDistance(item)
      })
      const newRent = maintain.concat(info)
      this.setData({
        maintain: newRent,
        isEmpty: Boolean(newRent.length),
        maintainTotal: res.data.total
      })
    }).catch((e) => {
      toast('请稍后再试', 1000)
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    toast('1',1000)
    const index = this.data.tabIndex
    const {
      rent,
      sell,
      maintain,
      rentTotal,
      maintainTotal,
      sellTotal 
    } = this.data
    if(index ===0) {
      this.setData({
        iDisplayStart: rent.length
      })
      rent.length === rentTotal ? toast('没有更多', 1000) : this.getRentCollect()
    } else if(index === 1) {
      this.setData({
        iDisplayStart: sell.length
      })
      sell.length === sellTotal ? toast('没有更多', 1000) : this.getSellCollect()
    } else {
      this.setData({
        iDisplayStart: maintain.length
      })
      maintain.length === maintainTotal ? toast('没有更多', 1000) : this.getMaintainCollect()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindTabChangeIndex(e) {
    const index = e.detail
    this.setData({
      tabIndex: index
    })
  },
  calcDistance(item) {
    const locationA = {
      lat: wx.getStorageSync('location').lat,
      lng: wx.getStorageSync('location').lng
    }
    const locationB = {
      lat: Number(item.lat),
      lng: Number(item.lon)
    }
    const distanceValue = distance(locationA, locationB)
    item = Object.assign({}, item, {
      distance: distanceValue
    })
    return item
  },
  bindDelete(e) {
    const _this = this
    const {
      index,
      inforid,
      id
    } = e.currentTarget.dataset
    const {
      tabIndex
    } = this.data
    wx.showModal({
      title: '温馨提示',
      content: '确认删除',
      success(res) {
        if (res.confirm) {
          Number(tabIndex) === 0 ? _this.rentDelete(index, id) : Number(tabIndex) === 1 ? _this.sellDelete(index, id) : _this.maintainDelete(index, id)
        } else if (res.cancel) {
          return false
        }
      }
    })

  },
  rentDelete(index, id) {
    const {
      rent
    } = this.data
    app.$api.collect.collectDelete({
      id,
      infortype: 1
    }).then((res) => {
      rent.splice(index, 1)
      this.setData({
        rent: rent
      })
      toast('删除成功', 1000)
    }).catch((e) => {
      toast('请稍后再试', 1000)
    })
  },
  sellDelete(index, id) {
    const {
      sell
    } = this.data
    app.$api.collect.collectDelete({
      id,
      infortype: 2
    }).then((res) => {
      sell.splice(index, 1)
      this.setData({
        sell: sell
      })
      toast('删除成功', 1000)
    }).catch((e) => {
      toast('请稍后再试', 1000)
    })
  },
  maintainDelete(index, id) {
    const {
      maintain
    } = this.data
    app.$api.collect.collectDelete({
      id,
      infortype: 3
    }).then((res) => {
      maintain.splice(index, 1)
      this.setData({
        maintain: maintain
      })
      toast('删除成功', 1000)
    }).catch((e) => {
      toast('请稍后再试', 1000)
    })
  },
  bindDetail(e) {
    const {id} = e.currentTarget.dataset
    const index = this.data.tabIndex
    wx.navigateTo({
      url: `/pages/detail/index?id=${id}&tabIndex=${index}`
    })
  }
})